package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.*;
import com.maochang.pojo.*;
import com.maochang.service.XiaoQueRenWuZhiXingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service("XiaoQueRenWuZhiXingServiceImpl")
public class XiaoQueRenWuZhiXingServiceImpl extends ServiceImpl<PsSolvetaskMainMapper, PsSolvetaskMain> implements XiaoQueRenWuZhiXingService {

    @Resource
    private PsSolvetaskMainMapper psSolvetaskMainMapper;
    @Resource
    private PsSysTemConfigMapper psSysTemConfigMapper;
    @Resource
    private PsInspectiontaskMainMapper psInspectiontaskMainMapper;
    @Resource
    private PsInspectiontaskDetailMapper psInspectiontaskDetailMapper;
    @Resource
    private PsSolvetaskDetailMapper psSolvetaskDetailMapper;
    @Resource
    private   PsSolverDetailMapper psSolverDetailMapper;
    /**
     * 任务执行 查询
     * @param ps
     * @param kai
     * @param jie
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<PsSolvetaskMain> ChaXjZhiXing(PsSolvetaskMain ps, String kai, String jie, int page, int size, HttpSession httpSession) {
        List<Integer>   shuZi=null;
        //1.表格初始化
        //2.表格带参数  执行中   中   或者    默认查询 的已经分配
        //当前用户 巡检员明细表
        PsUser yongHu = (PsUser) httpSession.getAttribute("loginUser");
        QueryWrapper<PsSolverDetail>  psSole= Wrappers.query();
        psSole.eq("solverName",yongHu.getUserName()).select("taskId");
        List<PsSolverDetail>   psSolverDetails= psSolverDetailMapper.selectList(psSole);
        System.out.println(psSolverDetails+"地方");
        shuZi=new ArrayList<>();
        for (PsSolverDetail psSolverDetail:psSolverDetails){
            //查询出来子表有的
            shuZi.add(psSolverDetail.getTaskId());
        }
        Page<PsSolvetaskMain> psliPage=new Page<>(page,size);
        //主表信息mapper
        QueryWrapper<PsSolvetaskMain> wrapperMain= Wrappers.query();
        //如果条件对象是个空
        if(ps==null||ps.equals("")){
            System.out.println("我走这个了");
            if(shuZi.size()>0){ //证明 这个  是有 巡检主任务信息的
                wrapperMain.like(!StringUtils.isBlank(ps.getSolveTaskCode()),"solveTaskCode",ps.getSolveTaskCode())
                        .eq(ps.getWorkDocType()!=null,"workDocType",ps.getWorkDocType())
                        .like(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
                        //.ne("taskStatusName","待分配")
                        .ge(!StringUtils.isBlank(kai),"issuedTime",kai)
                        .le(!StringUtils.isBlank(jie),"issuedTime",jie);
                        wrapperMain.in("id",shuZi);
            }else {
                wrapperMain.in("id","0");
            }
        }else {
            System.out.println("我这搜");
            if(shuZi.size()>0){ //证明 这个  是有 巡检主任务信息的
                System.out.println("数组大于0吗");
                wrapperMain.like(!StringUtils.isBlank(ps.getSolveTaskCode()),"solveTaskCode",ps.getSolveTaskCode())
                        .eq(ps.getWorkDocType()!=null,"workDocType",ps.getWorkDocType())
                        .like(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
                        .ge(!StringUtils.isBlank(kai),"issuedTime",kai)
                        .le(!StringUtils.isBlank(jie),"issuedTime",jie);
                      wrapperMain.in("id",shuZi);
            }else {
                wrapperMain.in("id","0");
            }
        }
        //当前用户
        //wrapperMain.ne("taskStatus","35");
        //wrapperMain.like(!StringUtils.isBlank(ps.getSolveTaskCode()),"solveTaskCode",ps.getSolveTaskCode())
        //        .eq(ps.getWorkDocType()!=null,"workDocType",ps.getWorkDocType())
        //        .like(!StringUtils.isBlank(ps.getIssuedByName()),"issuedByName",ps.getIssuedByName())
        //        .ge(!StringUtils.isBlank(kai),"issuedTime",kai)
        //        .le(!StringUtils.isBlank(jie),"issuedTime",jie);
        return psSolvetaskMainMapper.AllPXjList(psliPage,wrapperMain);
    }

    @Override
    public List<PsSystemconfig> ChaGongDan() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",12).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public PsInspectiontaskMain ChaZiTask(String solveTaskCode) {
        QueryWrapper<PsInspectiontaskMain> wrapper= Wrappers.query();
        wrapper.eq("inspectionTaskCode",solveTaskCode);
        PsInspectiontaskMain  psList =psInspectiontaskMainMapper.selectOne(wrapper);
        return psList;
    }

    @Override
    public List<PsInspectiontaskDetail> ChaZiTaskId(Integer id) {
        QueryWrapper<PsInspectiontaskDetail> wrapper= Wrappers.query();
        wrapper.eq("taskId",id);
        List<PsInspectiontaskDetail>  psList =psInspectiontaskDetailMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public boolean ZhiSiZhiXiu(PsSolvetaskMain id) {
        boolean a;
        QueryWrapper<PsSolvetaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(psSolvetaskMainMapper.ZhiSiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }

    @Override
    public List<PsSolvetaskDetail> ChaQueZiShu(Integer id) {
        QueryWrapper<PsSolvetaskDetail> wrapper= Wrappers.query();
        wrapper.eq("taskId",id);
        List<PsSolvetaskDetail>  psList =psSolvetaskDetailMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public List<PsInspectiontaskDetail> ChaXunZi(Integer[] inspectionTaskDelId) {
        QueryWrapper<PsInspectiontaskDetail> wrapper= Wrappers.query();
        wrapper.in("id", new Integer[][]{inspectionTaskDelId});
        List<PsInspectiontaskDetail>  psList =psInspectiontaskDetailMapper.selectList(wrapper);
        return psList;
    }

    @Override
    public boolean XiuGaiZhu(PsSolvetaskMain psSolvetaskMain) {
        QueryWrapper<PsSolvetaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",psSolvetaskMain.getId());
        int s=psSolvetaskMainMapper.update(psSolvetaskMain,wrapper);
        boolean fAlg=false;
        if (s>0){
            fAlg=true;
        }
        return fAlg;
    }

    @Override
    public boolean ShiJianSiZhiXiu(PsSolvetaskMain id) {
        boolean a;
        QueryWrapper<PsSolvetaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(psSolvetaskMainMapper.ShiJianSiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }

    @Override
    public boolean SiZhiXiu(PsSolvetaskMain id) {
        boolean a;
        QueryWrapper<PsSolvetaskMain> wrapper=Wrappers.query();
        wrapper.eq("id",id.getId());
        if(psSolvetaskMainMapper.SiZhiXiu(id)){
            a=true;
        }else {
            a=false;
        }
        return a;
    }

    @Override
    public List<PsSystemconfig> ChaRenWuZhuang() {
        QueryWrapper<PsSystemconfig> wrapper= Wrappers.query();
        wrapper.eq("parentId",14).orderByAsc("id");
        List<PsSystemconfig>  psList =psSysTemConfigMapper.selectList(wrapper);
        return psList;
    }

}
